home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pbaseiv.zip / P4MSC008.TIP < prev    next >
Text File  |  1991-12-16  |  3KB  |  91 lines

  1. In a sense, a running BASIC program is lost--it doesn't know
  2. its name or its path, a fact that poses a problem for
  3. programs that must access files in their home directories. I
  4. solved that problem by taking advantage of a little-known
  5. DOS feature.
  6.  
  7. When DOS runs a program, it stores the program's path and
  8. name after the last environment string. For a BASIC program
  9. to find its roots, it must locate the Program Segment Prefix
  10. (PSP), which is attached to the program at run time. From
  11. the PSP, the program can find out the directory from which
  12. it was loaded. It's then a simple matter to lop off the
  13. path, attach the file names you need, and use the results to
  14. access data in the program's home directory. I've included a
  15. sample program [see listing below] that shows how this is
  16. done. [You can copy the listing to a file by pressing the
  17. Alt-F key.]
  18.  
  19. H. B. Clyde, Jr.
  20. Incline Village, Nevada
  21.  
  22. Editor's note: I modified Mr. Clyde's program so you can
  23. compile it with the popular QuickBASIC 4.5. When run, the
  24. program displays its directory path and file name.
  25.  
  26. Before compiling, start QuickBASIC with the /L parameter so
  27. that the Quick Library QB.QLM, which contains support
  28. routines needed to call DOS functions, is also loaded. The
  29. program uses one of those routines, CALL Interrupt, to
  30. locate the PSP.
  31.  
  32. In the listing, comments beginning with an apostrophe
  33. explain how the program works. The second line, however, is
  34. not a comment but a command to include QB.BI, which defines
  35. RegType, a structure that stores pseudo register values to
  36. pass to DOS and to receive information back.
  37.  
  38.  
  39. FINDPATH.BAS demonstrates how a QuickBASIC program can
  40. locate its own path.
  41.  
  42. ---- BEGIN LISTING ----
  43. ' FINDPATH by H. B. Clyde, Jr.
  44. ' $INCLUDE: 'QB.BI'
  45. ' Find Prog Seg Prefix (PSP)
  46. DIM regs AS RegType
  47. regs.ax = &H6200
  48. CALL Interrupt(&H21,regs,regs)
  49. ' Locate program's environment
  50. PSPSegment = regs.bx
  51. def seg = PSPSegment
  52. offset = &H2C
  53. LoAdd = peek(offset)
  54. HiAdd = peek(offset + 1)
  55. EnvSeg = LoAdd + (256 * HiAdd)
  56. ' Find command string
  57. def seg = EnvSeg
  58. x = 0
  59. while peek(x) + peek(x+1) <> 0
  60.   x = x + 1
  61. wend
  62. ' Find end of program's path
  63. StartField = x + 4
  64. EndField = StartField
  65. while peek(EndField) <> 0
  66.   EndField = EndField + 1
  67. wend
  68. ' Locate backslash at path end
  69. EndPath = EndField
  70. while peek(EndPath) <> &H5C
  71.   EndPath = EndPath - 1
  72. wend
  73. ' Copy path name to string
  74. for x = StartField to EndPath
  75.   Path$ = Path$ + chr$(peek(x))
  76. next x
  77. print "Path = "Path$
  78. ' Copy program name to string
  79. for x = EndPath + 1 to EndField
  80.   Prog$ = Prog$ + chr$(peek(x))
  81. next x
  82. print "Program = " Prog$
  83. end
  84. ---- END LISTING ----
  85.  
  86. Title: Where Am I?
  87. Category: MSC
  88. Issue date: May 1991
  89. Editor: Tom Swan
  90. Supplementary files: NONE
  91.